package com.bnzjy.srb.core.controller.api;

import com.bnzjy.common.exception.Assert;
import com.bnzjy.common.result.R;
import com.bnzjy.common.result.ResponseEnum;
import com.bnzjy.common.util.RegexValidateUtils;
import com.bnzjy.srb.base.util.JwtUtils;
import com.bnzjy.srb.core.pojo.vo.BorrowerVO;
import com.bnzjy.srb.core.service.BorrowerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Api(tags = "借款人")
@RestController
@RequestMapping("/api/core/borrower")
@Slf4j
public class ApiBorrowerController {
    
    @Resource
    BorrowerService borrowerService;
    
    
    /**
     * 前端借款人表单数据提交
     * @param borrowerVO
     * @param request
     * @return
     */
    @ApiOperation("保存借款人信息")
    @PostMapping("/auth/save")
    public R save(@Valid @RequestBody BorrowerVO borrowerVO, HttpServletRequest request) {
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        String contactsMobile = borrowerVO.getContactsMobile();
        String contactsName = borrowerVO.getContactsName();

        // 联系人姓名
        Assert.notEmpty(contactsName, ResponseEnum.CONTACT_NULL_ERROR);
        // 联系人手机不能为空,
        Assert.notEmpty(contactsMobile, ResponseEnum.MOBILE_NULL_ERROR);
        // 联系人手机号格式不正确
        Assert.isTrue(RegexValidateUtils.checkCellphone(contactsMobile), ResponseEnum.MOBILE_ERROR);
        
        borrowerService.saveBorrowerVOByUserId(borrowerVO, userId);
        return R.ok().message("信息提交成功");
    }
    
    /**
     * 获取借款人状态
     * @param request
     * @return
     */
    @ApiOperation("获取借款人认证状态")
    @GetMapping("/auth/getBorrowerStatus")
    public R getBorrowerStatus(HttpServletRequest request){
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        Integer status = borrowerService.getStatusByUserId(userId);
        return R.ok().data("borrowerStatus", status);
    }
    
}
